package com.util;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.Properties;

public class DBHelper {

    //1+2
    public Connection getCon() {
        Connection con = null;
        try {

            Properties properties=new Properties();
            InputStream is= DBHelper.class.getClassLoader().getResourceAsStream("com/util/jdbc.properties");
            properties.load(is);

            Class.forName(properties.getProperty("mysql.driver"));
            String url = properties.getProperty("mysql.url");
            String username = properties.getProperty("mysql.username");
            String password = properties.getProperty("mysql.password");
            con = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            System.out.println("mysql 驱动类地址不对，没有导入jar url路径不对");
            //语法，逻辑，需求错误
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return con;
    }
    //7
    public void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
        try {
            if (rs != null) rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (ps != null) ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (con != null) con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //1-7
    public ArrayList selectinfo(String sql, Class cla, Object... objs) {
        ArrayList list = new ArrayList<>();
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                //根据cla，获得字节码对应类型下的对象
                Object o = cla.newInstance();
                Field[] declaredFields = cla.getDeclaredFields();
                //2.给对象属性赋值
                for (Field f : declaredFields) {
                    f.setAccessible(true);
                    f.set(o, rs.getObject(f.getName()));
                }
                list.add(o);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return list;
    }

    /**
     * 求符合条件的数据的总个数
     *
     * @param sql
     * @param objs
     * @return
     */
    public int selectintfoCount(String sql, Object... objs) {

        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            rs = ps.executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return count;
    }
    public int update(String sql, Object... objs) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll(con, ps, rs);
        }
        return count;

    }
}
